const { HW_AD_CONFIG } = require("../constants");

module.exports = {
    adVideoDict: {},
    adVideoExist: false,
    interstitialExist: false,
    videoFunc: null,
    nativeDict: {},
    nativeJson: null,
    nativeCurrent: null,        // 当前广告
    nativeFuc: null,             // 广告函数
    init() {
        this.initVideo()
        //this.initNativeAd()
    },
    initVideo() {
        if (!HW_AD_CONFIG.videoId.length) {
            return
        }
        for (let i = 0; i < HW_AD_CONFIG.videoId.length; i++) {
            this.adVideoDict[HW_AD_CONFIG.videoId[i]] = hbs.createRewardedVideoAd({
                adUnitId: HW_AD_CONFIG.videoId[i], success: (code) => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd: success");
                },
                fail: (data, code) => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd fail: " + data + "," + code);
                },
                complete: () => {
                    console.log("ad demo : loadAndShowVideoAd createRewardedVideoAd complete");
                }
            })

            this.adVideoDict[HW_AD_CONFIG.videoId[i]].onLoad((res) => {
                this.adVideoExist = true
                // this.adVideoDict[HW_AD_CONFIG.videoId[0]].show()
            })
            this.adVideoDict[HW_AD_CONFIG.videoId[i]].onError(err => {
                console.error(err, HW_AD_CONFIG.videoId[i], '失败')
            })
            this.adVideoDict[HW_AD_CONFIG.videoId[i]].onClose(res => {
                this.adVideoExist = false
                if (res && res.isEnded || res === undefined) {
                    if (this.videoFunc) {
                        he.local.click("load_ad_get")
                        this.videoFunc()
                        this.videoFunc = null
                    }
                    // gg.data.updateTask(9001)
                }
                this.loadVideo(HW_AD_CONFIG.videoId[i])
            })
        }
        this.loadVideo()
    },
    // 加载广告
    loadVideo(id) {
        if (id) {
            if (!this.adVideoExist) {
                this.adVideoDict[id].load()
            }
        } else {
            for (let key in this.adVideoDict) {
                this.adVideoDict[key].load()
            }
        }

    },
    // 显示视频
    showVideo(fuc) {
        if (!this.adVideoExist) {
            gg.uiManager.addTip("未获取到广告,请稍后重试")
            this.loadVideo(HW_AD_CONFIG.videoId[0])
            return
        }
        // he.local.click("load_ad_click")
        this.videoFunc = fuc
        this.adVideoDict[HW_AD_CONFIG.videoId[0]].show()
    },
    // 初始化记录原始广告
    initNativeAd() {
        for (let i = 0; i < HW_AD_CONFIG.nativeAd.length; i++) {
            let nativeAdId = HW_AD_CONFIG.nativeAd[i]
            this.nativeDict[nativeAdId] = hbs.createNativeAd({
                adUnitId: nativeAdId,
                success: (code) => {
                    this.nativeDict[nativeAdId].load()
                },
            })
            this.nativeDict[nativeAdId].onLoad((res) => {
                console.log(nativeAdId, '加载***********', res, res.adList, '_____________________')
                if (res && res.adList) {
                    this.nativeJson[nativeAdId] = res.adList[res.adList.length - 1]
                }
            })
            this.nativeDict[nativeAdId].onError((err) => {
                console.log(nativeAdId, '失败***********', err, '_____________________')
            })
        }
    },
    // 获得原生
    getNativeJson(id, fuc, errfuc) {
        console.log('创建yuansheng gguang       ', id)
        if (this.nativeCurrent && this.nativeCurrent == id && this.nativeJson) {
            if (!gg.data.reprotAd) {
                gg.data.reprotAd = true
            } else {
                return
            }
            console.log('吊东西已经有了', id)
            this.nativeDict[this.nativeCurrent].reportAdShow({ adId: this.nativeJson.adId.toString() })
            return this.nativeJson
        } else {
            // console.log('创建一个吊东西', id)
            this.nativeFuc = fuc
            this.nativeErrFuc = errfuc
            if (this.nativeDict[id]) {
                console.log('加载吧')
                if (HW_AD_CONFIG.nativeAd.indexOf(id) < 0) {
                    return false
                } else {
                    this.nativeCurrent = id
                    this.nativeDict[this.nativeCurrent].load()
                    this.nativeDict[this.nativeCurrent].onLoad((res) => {
                        if (res && res.adList) {
                            console.log('Load成功', res)
                            this.nativeJson = res.adList[res.adList.length - 1]
                        }
                        else
                            this.nativeJson = null
                    })
                    return this.nativeJson
                }
            } else {
                if (HW_AD_CONFIG.nativeAd.indexOf(id) < 0) {
                    return null
                } else {
                    // console.log('创建吧',id)
                    this.nativeCurrent = id
                    this.nativeDict[id] = hbs.createNativeAd({
                        adUnitId: id,
                        success: (code) => {
                            this.nativeDict[id].load()
                        },
                    })
                    this.nativeDict[id].onLoad((res) => {
                        if (res && res.adList)
                            this.nativeJson = res.adList[res.adList.length - 1]
                        else
                            this.nativeJson = null
                    })
                    return this.nativeJson
                }
            }
        }
    },
    onErrorNative(err) {
        if (err) {
            console.log(err)
        }
        if (this.nativeErrFuc) {
            this.nativeErrFuc()
        }
    },
    // 进入原生
    clickNative(id, call) {
        if (this.nativeCurrent && this.nativeCurrent == id && this.nativeJson) {
            this.nativeDict[id].reportAdClick({ adId: this.nativeJson.adId.toString() })
            this.nativeCurrent = null
            this.nativeJson = null
            he.log("点击广告", id)
            call()
        } else {
            he.warn("妹有广告", id)
        }
    }
}
